home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Devil's Cubes 1.0.1 / source / Devil’s Cubes ƒ / MSG Shell ƒ / msg main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  4.8 KB  |  217 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg main.c
  4.  
  5. Purpose:    This module handles the event loop and event dispatching.
  6.  
  7.  
  8. Devil’s Cubes -- a simple cubes puzzle
  9. Copyright (C) 1993 Mark Pilgrim
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "msg main.h"
  29. #include "msg integrity.h"
  30. #include "msg graphics.h"
  31. #include "msg menus.h"
  32. #include "msg sounds.h"
  33. #include "msg prefs.h"
  34. #include "msg environment.h"
  35. #include "cube.h"
  36. #include "AppleEvents.h"
  37. #include "EPPC.h"
  38.  
  39. void main(void)
  40. {
  41.     MaxApplZone();    
  42.     InitGraf(&thePort);
  43.     InitFonts();
  44.     FlushEvents(everyEvent, 0);
  45.     InitWindows();
  46.     InitMenus();
  47.     TEInit();
  48.     InitDialogs(0L);
  49.     InitCursor();
  50.     GetDateTime(&randSeed);
  51.     
  52.     CheckEnvironment();    
  53.     DoIntegrityCheck();    
  54.     InitSounds();
  55.     PrefsError(PreferencesInit());
  56.     InitEnvironment();    
  57.     InitGame();
  58.     EventLoop();    
  59.     ShutDownEnvironment();
  60.     ExitToShell();
  61. }
  62.  
  63. void EventLoop(void)
  64. {
  65.     EventRecord        theEvent;
  66.     
  67.     while (!gDone)
  68.     {
  69.         SetCursor(&arrow);
  70.         HiliteMenu(0);
  71.         
  72.         if(gMainWindow)
  73.             SetPort(gMainWindow);
  74.         
  75.         if (WaitNextEvent(everyEvent, &theEvent, 10, 0L))
  76.             DispatchEvents(theEvent);
  77.     }
  78. }
  79.  
  80. void DispatchEvents(EventRecord theEvent)
  81. {
  82.     int                i;
  83.     OSErr            isHuman;
  84.     Point            thisPoint;
  85.     
  86.     switch (theEvent.what)
  87.     {
  88.         case mouseDown:
  89.             HandleMouseDown(theEvent);
  90.             break;
  91.         case keyDown:
  92.         case autoKey:
  93.             if(theEvent.modifiers & cmdKey)
  94.             {
  95.                 AdjustMenus();
  96.                 HandleMenu(MenuKey((char)(theEvent.message & charCodeMask)));
  97.             }
  98.             else if (gMainWindow)
  99.                 GameKeyEvent((char)(theEvent.message & charCodeMask));
  100.             break;
  101.         case diskEvt:
  102.             if (HiWord(theEvent.message)!=noErr)
  103.             {
  104.                 DILoad();
  105.                 SetPt(&thisPoint, 120, 120);
  106.                 isHuman=DIBadMount(thisPoint, theEvent.message);
  107.                 DIUnload();
  108.             }
  109.             break;
  110.         case updateEvt:
  111.             BeginUpdate((WindowPtr)theEvent.message);
  112.             
  113.             if((WindowPtr)theEvent.message == gMainWindow)
  114.                 UpdateBoard();
  115.             
  116.             for (i=0; i<NUM_HELP; i++)
  117.                 if ((WindowPtr)theEvent.message == gHelp[i])
  118.                     UpdateHelp(i);
  119.             
  120.             EndUpdate((WindowPtr)theEvent.message);
  121.             break;
  122.         case kHighLevelEvent:
  123.             if (gHasAppleEvents)
  124.                 AEProcessAppleEvent(&theEvent);
  125.             break;
  126.     }
  127. }
  128.  
  129. void HandleMouseDown(EventRecord theEvent)
  130. {
  131.     WindowPtr            theWindow;
  132.     int                    windowCode;
  133.     long                windSize;
  134.     GrafPtr                oldPort;
  135.     int                    i;
  136.     Rect                sizeRect;
  137.     
  138.     windowCode=FindWindow(theEvent.where, &theWindow);
  139.     switch (windowCode)
  140.     {
  141.         case inMenuBar:
  142.             AdjustMenus();
  143.             HandleMenu(MenuSelect(theEvent.where));
  144.             break;
  145.         case inContent:
  146.             if(FrontWindow() != theWindow)
  147.                 SelectWindow(theWindow);
  148.             else
  149.                 if(theWindow == gMainWindow)
  150.                     GameEvent();
  151.             break;
  152.         case inSysWindow:
  153.             SystemClick(&theEvent, theWindow);
  154.             break;
  155.         case inDrag:
  156.             DragWindow(theWindow, theEvent.where, &gDragRect);
  157.             if(theWindow == gMainWindow)
  158.                 gMainWindowBounds = (*(((WindowPeek)gMainWindow)->contRgn))->rgnBBox;
  159.             break;
  160.         case inGoAway:
  161.             if (TrackGoAway(theWindow, theEvent.where))
  162.             {
  163.                 for (i=0; i<NUM_HELP; i++)
  164.                     if (theWindow == gHelp[i])
  165.                         gHelp[i]=0L;
  166.                 
  167.                 if(theWindow == gMainWindow)
  168.                     CloseMainWindow();
  169.                 else
  170.                     DisposeWindow(theWindow);
  171.                 
  172.                 AdjustMenus();
  173.             }
  174.             break;
  175.         case inGrow:
  176.             sizeRect = screenBits.bounds;
  177.             OffsetRect(&sizeRect, sizeRect.left, sizeRect.top);
  178.             
  179.             windSize = GrowWindow(theWindow, theEvent.where, &sizeRect);
  180.             if(windSize != 0)
  181.             {
  182.                 GetPort(&oldPort);
  183.                 SetPort(theWindow);
  184.                 EraseRect(&theWindow->portRect);
  185.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  186.                 InvalRect(&theWindow->portRect);
  187.                 SetPort(oldPort);
  188.             }
  189.             
  190.             if(theWindow == gMainWindow)
  191.                 gMainWindowBounds = (*(((WindowPeek)gMainWindow)->contRgn))->rgnBBox;
  192.             
  193.             break;
  194.         case inZoomIn:
  195.         case inZoomOut:
  196.             if(TrackBox(theWindow, theEvent.where, windowCode))
  197.             {
  198.                 GetPort(&oldPort);
  199.                 SetPort(theWindow);
  200.                 ZoomWindow(theWindow, windowCode, FALSE);
  201.                 InvalRect(&theWindow->portRect);
  202.                 SetPort(oldPort);
  203.             }
  204.             
  205.             if(theWindow == gMainWindow)
  206.                 gMainWindowBounds = (*(((WindowPeek)gMainWindow)->contRgn))->rgnBBox;
  207.             
  208.             break;
  209.     }
  210. }
  211.  
  212. void ShutDownEnvironment(void)
  213. {
  214.     ShutDownGame();
  215.     CloseSounds();    
  216. }
  217.